package com.szholly.data.general;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.metadata.table.TableRelationRow;

/**
 * 类功能说明 定义表关系map关系
 * 2014.6.5
 */
public class RelationMap implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7273161850604461948L;
	
	private HashMap<String, RelationNode> map;

	/**
	 * 初始化表关系
	 * 
	 * @param relations
	 */
	public RelationMap(List<RowBase> relations) {
		map = new HashMap<String, RelationNode>();
		initRelation(relations);
	}

	/**
	 * 初始化表关系
	 * 
	 * @param relations
	 */
	protected void initRelation(List<RowBase> relations) {
		if (relations == null || relations.size() <= 0)
			return;

		List<RowBase> relationsR = relations;
		// 所有表关系 TableRelationRow
		for (RowBase entity : relationsR) {
			String pTable = TableRelationRow.getPTableID(entity);
			pTable = pTable.toUpperCase().trim();
			String cTable = TableRelationRow.getCTableID(entity);
			String foreignKey = TableRelationRow.getForeignKey(entity);

			if (StringUtils.IsNullOrSpace(pTable)
					|| StringUtils.IsNullOrSpace(cTable)
					|| StringUtils.IsNullOrSpace(foreignKey))
				continue;

			RelationInfo relationInfo = new RelationInfo();
			relationInfo.RelationStrength = RelationStrength
					.getTableTypeByTypeId(TableRelationRow.getRelationStrength(entity));
			relationInfo.RelationType = RelationType
					.getTableTypeByTypeId(TableRelationRow.getRelationType(entity));
			relationInfo.Foreignkey = new HashMap<String, String>();
			// 处理关系
			String[] keys = foreignKey.split(",");
			for (String key : keys) {
				String[] oneKeys = key.split("=");
				if (oneKeys.length != 2)
					continue;

				relationInfo.Foreignkey.put(oneKeys[0], oneKeys[1]);
			}

			// 记录信息
			if (map.containsKey(pTable))
				map.get(pTable).getChildRelationNode()
						.put(cTable, relationInfo);
			else {
				RelationNode node = new RelationNode(pTable);
				node.getChildRelationNode().put(cTable, relationInfo);
				map.put(pTable, node);
			}
		}

		// 通过子表，给父表赋值
		Set<String> item = map.keySet();
		for (String itemKey : item) {
			HashMap<String, RelationInfo> childs = map.get(itemKey)
					.getChildRelationNode();

			Set<String> childsKeys = childs.keySet();
			for (String childsKey : childsKeys) {
				RelationInfo relationInfo = new RelationInfo();
				relationInfo.Foreignkey = new HashMap<String, String>();
				relationInfo.RelationStrength = childs.get(childsKey).RelationStrength;
				relationInfo.RelationType = childs.get(childsKey).RelationType;

				HashMap<String, String> forIitems = childs.get(childsKey).Foreignkey;
				Set<String> forIitemKeys = forIitems.keySet();
				for (String forIitemKey : forIitemKeys) {
					relationInfo.Foreignkey.put(forIitems.get(forIitemKey),
							forIitemKey);
				}
			}
		}
	}

	/**
	 * 获取表关系节点列表中的指定节点
	 * 
	 * @param tablePK
	 *            表唯一标识
	 * @return 所查找的节点
	 */
	public RelationNode GetNode(String tablePK) {
		if (StringUtils.IsNullOrSpace(tablePK))
			return null;

		tablePK = tablePK.toUpperCase().trim();
		if (map.containsKey(tablePK))
			return map.get(tablePK);
		else
			return null;
	}
}
